package com.mjh.listener;

import ch.qos.logback.core.util.ContextUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.mjh.constant.AuthConstants;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;


import java.util.Set;

/**
 * 关闭后台服务器时清除redis中的令牌TOKEN
 */
@Slf4j
@Component
public class ShutDownListener implements ApplicationListener<ContextClosedEvent> {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        log.info("关闭后台服务器时清除redis中的令牌TOKEN");
        Set<String> keys = stringRedisTemplate.keys(AuthConstants.TOKEN_REDIS_KEY_PREFIX + "*");
        if(CollectionUtil.isNotEmpty(keys)){
            stringRedisTemplate.delete(keys);
        }
    }
}
